﻿using AutoMapper;
using MediatR;
using Traceability.App.Api.Read.Application.Command.KillCompany;
using Traceability.App.Domain.养殖基本信息表;
using Traceability.App.Domain.屠宰信息表;
using Traceability.App.ErrorCode;
using Traceability.App.Intrastructure.Interface;

namespace Traceability.App.Api.Read.Application.Handler.KillCompany
{
    public class KillCompanyHandler:IRequestHandler<KillCompanyCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<KillCompanyModel> KillcompanyRepo;
        private readonly IMapper mapper;

        public KillCompanyHandler(IBaseRepository<KillCompanyModel> KillcompanyRepo, IMapper mapper)
        {
            this.KillcompanyRepo = KillcompanyRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(KillCompanyCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();
            var entity = mapper.Map<KillCompanyModel>(request);
            var r = KillcompanyRepo.Add(entity).Result;
            if (r > 0)
            {
                res.Message = "新增成功";
                res.Code = ApiEnum.Ok;
                res.Data = r;
            }
            else
            {
                res.Message = "新增失败";
                res.Code = ApiEnum.Error;
                res.Data = r;
            }
            return Task.FromResult(res);
        }
    }
}
